package com.Controller;

import cn.hutool.core.util.StrUtil;
import com.Bean.User;
import com.Common.CodeMsg;
import com.Common.Constant;
import com.Common.Result;
import com.Service.UserService;
import com.Service.impl.UserServiceImpl;
import com.Util.RespUtil;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: hkw14
 * @time: 2021/4/28 18:02
 */
@WebServlet("/user.do")
public class UserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    private static final long serialVersionUID = -5124288860178782422L;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String type = req.getParameter("type");
        try {
            //利用反射调用方法
            this.getClass().getDeclaredMethod(type, HttpServletRequest.class,HttpServletResponse.class)
                    .invoke(this, req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void login(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //获取用户名
        String userName = req.getParameter("username");
        //获取密码
        String password = req.getParameter("password");
        Result rs = userService.login(userName, password);
        if(rs.getCode() == CodeMsg.SUCCESS.getCode()) {
            req.getSession().setAttribute(Constant.CURRENT_USER, rs.getData());
        }
        RespUtil.write(resp, rs);
    }

    protected void exit(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.getSession().removeAttribute(Constant.CURRENT_USER);
        resp.sendRedirect("index.jsp");
    }

    /**
     * @description: 返回分页用户数据
     * @Params: * @param req
     * @param resp
     * @return:
     * @author: hkw14
     * @time: 2021/4/30 16:17
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String limit = req.getParameter("limit");
        String page = req.getParameter("page");
        String userName = req.getParameter("userName");
        String realName = req.getParameter("realName");
        String role = req.getParameter("role");
        Map<String,Object> params = new HashMap<String,Object>();

        if(StrUtil.isNotBlank(userName)) {
            params.put("userName", userName);
        }
        if(StrUtil.isNotBlank(realName)) {
            params.put("realName", realName);
        }
        if(StrUtil.isNotBlank(role)) {
            params.put("role", role);
        }

        Result result = userService.queryPage(params, page, limit);
        RespUtil.write(resp, result);
    }


    protected void reset(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Result rs = userService.reset(id);
        RespUtil.write(resp, rs);
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String account = req.getParameter("userName");
        String realName = req.getParameter("realName");
        String role = req.getParameter("role");
        String password = req.getParameter("password");
        Result rs = userService.addUser(account,password,realName,role);
        RespUtil.write(resp, rs);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] ids = req.getParameterValues("ID");
        System.out.println(Arrays.toString(ids));
        Result rs = userService.deleteUser(ids);
        RespUtil.write(resp, rs);
    }
}
